<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
    <title>管理员控制台</title>
    <script src="https://cdn.tailwindcss.com/3.4.16"></script>
    <style>
        .sidebar {
            scrollbar-width: thin;
            scrollbar-color: #6366f1 #f1f5f9;
        }
        .sidebar::-webkit-scrollbar {
            width: 6px;
        }
        .sidebar::-webkit-scrollbar-track {
            background: #f1f5f9;
        }
        .sidebar::-webkit-scrollbar-thumb {
            background-color: #6366f1;
            border-radius: 3px;
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen flex">
<!-- 侧边导航栏 -->
<div class="w-64 bg-indigo-800 text-white flex-shrink-0 hidden md:block">
    <div class="p-4 border-b border-indigo-700">
        <h1 class="text-xl font-bold">书籍投票系统</h1>
        <p class="text-sm text-indigo-300">管理员控制台</p>
    </div>
    <nav class="p-4 space-y-2">
        <a href="#" class="block px-4 py-2 bg-indigo-700 rounded flex items-center gap-2">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                <path d="M9 6a3 3 0 11-6 0 3 3 0 016 0zM17 6a3 3 0 11-6 0 3 3 0 016 0zM12.93 17c.046-.327.07-.66.07-1a6.97 6.97 0 00-1.5-4.33A5 5 0 0119 16v1h-6.07zM6 11a5 5 0 015 5v1H1v-1a5 5 0 015-5z" />
            </svg>
            书籍管理
        </a>
        <a href="records" class="block px-4 py-2 hover:bg-indigo-700 rounded flex items-center gap-2 transition">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd" />
            </svg>
            投票记录
        </a>
        <a href="logout" class="block px-4 py-2 hover:bg-indigo-700 rounded flex items-center gap-2 transition">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M3 3a1 1 0 00-1 1v12a1 1 0 102 0V4a1 1 0 00-1-1zm10.293 9.293a1 1 0 001.414 1.414l3-3a1 1 0 000-1.414l-3-3a1 1 0 10-1.414 1.414L14.586 9H7a1 1 0 100 2h7.586l-1.293 1.293z" clip-rule="evenodd" />
            </svg>
            退出登录
        </a>
    </nav>
</div>

<!-- 主内容区 -->
<div class="flex-1 flex flex-col overflow-hidden">
    <!-- 顶部导航-->
    <header class="bg-white border-b md:hidden">
        <div class="px-4 py-3 flex justify-between items-center">
            <h1 class="text-lg font-semibold">书籍投票系统</h1>
            <button class="p-1 rounded-md text-gray-500 hover:text-gray-600 focus:outline-none">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
                </svg>
            </button>
        </div>
    </header>

    <!-- 内容区 -->
    <main class="flex-1 overflow-y-auto p-4 md:p-6">
        <!-- 消息提示 -->
        <c:if test="${not empty message}">
            <div class="mb-6 p-3 bg-green-100 border-l-4 border-green-500 text-green-700 rounded">
                    ${message}
            </div>
        </c:if>
        <c:if test="${not empty error}">
            <div class="mb-6 p-3 bg-red-100 border-l-4 border-red-500 text-red-700 rounded">
                    ${error}
            </div>
        </c:if>

        <!-- 主标题和操作按钮 -->
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-6">
            <h2 class="text-2xl font-bold text-gray-800 mb-4 sm:mb-0">书籍管理</h2>
            <button id="add-book-toggle" class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded transition flex items-center justify-center gap-2">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                    <path fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" clip-rule="evenodd" />
                </svg>
                添加新书籍
            </button>
        </div>

        <!-- 添加书籍表单-->
        <div id="add-book-form" class="hidden mb-8 bg-white p-6 rounded-lg shadow">
            <h3 class="text-lg font-semibold mb-4">添加新书籍</h3>
            <form action="admin" method="post" class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <input type="hidden" name="action" value="add">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">书籍ID</label>
                    <input type="text" name="id" required class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">书名</label>
                    <input type="text" name="title" required class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">作者</label>
                    <input type="text" name="author" required class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                </div>
                <div class="md:col-span-3 flex justify-end">
                    <button type="submit" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded transition">
                        确认添加
                    </button>
                </div>
            </form>
        </div>

        <!-- 书籍列表 -->
        <c:if test="${not empty books}">
            <c:set var="totalVotes" value="0" />
            <c:forEach items="${books}" var="book">
                <c:set var="totalVotes" value="${totalVotes + book.votes}" />
            </c:forEach>

            <div class="bg-white rounded-lg shadow overflow-hidden">
                <div class="grid grid-cols-12 bg-gray-100 p-3 font-medium text-gray-700">
                    <div class="col-span-4 md:col-span-5">书名</div>
                    <div class="col-span-3 md:col-span-2 text-center">作者</div>
                    <div class="col-span-2 text-center">票数</div>
                    <div class="col-span-2 text-center">占比</div>
                    <div class="col-span-1 text-center">操作</div>
                </div>

                <c:forEach items="${books}" var="book">
                    <div class="grid grid-cols-12 p-3 border-b hover:bg-gray-50 items-center">
                        <div class="col-span-4 md:col-span-5 font-medium">《${book.title}》</div>
                        <div class="col-span-3 md:col-span-2 text-gray-600 text-center">${book.author}</div>
                        <div class="col-span-2 text-indigo-600 text-center">${book.votes}</div>
                        <div class="col-span-2 text-indigo-600 text-center">
                            <fmt:formatNumber value="${book.votes / totalVotes * 100}" pattern="#"/>%
                        </div>
                        <div class="col-span-1 flex justify-center space-x-1">
                            <button data-book-id="${book.id}" data-book-title="${book.title}" data-book-author="${book.author}"
                                    class="update-book-btn p-1 text-indigo-600 hover:text-indigo-800">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                    <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
                                </svg>
                            </button>
                            <form action="admin" method="post" class="inline">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="bookId" value="${book.id}">
                                <button type="submit" class="p-1 text-red-600 hover:text-red-800">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                        <path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" />
                                    </svg>
                                </button>
                            </form>
                        </div>
                    </div>
                </c:forEach>
            </div>
        </c:if>

        <!-- 更新书籍模态框 -->
        <div id="update-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 z-50">
            <div class="bg-white rounded-lg shadow-xl w-full max-w-md">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4">更新书籍信息</h3>
                    <form action="admin" method="post" id="update-form" class="space-y-4">
                        <input type="hidden" name="action" value="update">
                        <input type="hidden" name="id" id="update-book-id">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">书名</label>
                            <input type="text" name="title" id="update-book-title" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">作者</label>
                            <input type="text" name="author" id="update-book-author" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>
                        <div class="flex justify-end space-x-3 pt-4">
                            <button type="button" id="cancel-update" class="px-4 py-2 border border-gray-300 text-gray-700 rounded hover:bg-gray-100 transition">
                                取消
                            </button>
                            <button type="submit" class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded transition">
                                确认更新
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</div>

<script>
    // 添加书籍表单切换
    document.getElementById('add-book-toggle').addEventListener('click', function() {
        const form = document.getElementById('add-book-form');
        form.classList.toggle('hidden');
    });

    // 更新书籍模态框
    const updateBtns = document.querySelectorAll('.update-book-btn');
    const updateModal = document.getElementById('update-modal');

    updateBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            document.getElementById('update-book-id').value = this.dataset.bookId;
            document.getElementById('update-book-title').value = this.dataset.bookTitle;
            document.getElementById('update-book-author').value = this.dataset.bookAuthor;
            updateModal.classList.remove('hidden');
        });
    });

    document.getElementById('cancel-update').addEventListener('click', function() {
        updateModal.classList.add('hidden');
    });
</script>
</body>
</html>